package cc.admin.modules.sys.service.impl;

import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.shiro.authc.ShiroRealm;
import cc.admin.modules.sys.entity.SysPermission;
import cc.admin.modules.sys.entity.SysRolePermission;
import cc.admin.modules.sys.entity.SysShipType;
import cc.admin.modules.sys.entity.SysShipTypePermission;
import cc.admin.modules.sys.mapper.SysShipTypeMapper;
import cc.admin.modules.sys.mapper.SysShipTypePermissionMapper;
import cc.admin.modules.sys.service.ISysPermissionService;
import cc.admin.modules.sys.service.ISysShipTypePermissionService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysShipTypePermissionServiceImpl extends ServiceImpl<SysShipTypePermissionMapper, SysShipTypePermission> implements ISysShipTypePermissionService {
    @Resource
    private ISysPermissionService sysPermissionService;

    @Override
    public void saveShipTypePermission(String shipType, String permissionIds, String lastPermissionIds) {
        // 删除角色权限
        this.remove(new QueryWrapper<SysShipTypePermission>().lambda().eq(SysShipTypePermission::getShipTypeCode, shipType));
        List<String> permissionList = StrUtil.splitTrim(permissionIds,",");
        List<SysShipTypePermission> sysShipTypePermissions= new ArrayList<>();
        List<SysShipTypePermission> list = addParentId(shipType, permissionList,sysShipTypePermissions);

        this.saveBatch(list);

        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        ShiroRealm shiroRealm = (ShiroRealm) securityManager.getRealms().iterator().next();
        //清除权限 相关的缓存
        shiroRealm.clearAllCache();
    }


    private  List<SysShipTypePermission> addParentId(String shipType,List<String> permissionList, List<SysShipTypePermission> list ){
        LoginUser sysUser= (LoginUser)SecurityUtils.getSubject().getPrincipal();
        List<String> permissionParentList=new ArrayList<>();
        for (String permissionId:permissionList){
            SysPermission sysPermission= sysPermissionService.getById(permissionId);
            String parentId="";
            if (sysPermission!=null){
                parentId=sysPermission.getParentId();
            }

            if(StrUtil.isNotEmpty(parentId)){
                if (!permissionParentList.contains(parentId)){
                    permissionParentList.add(parentId);
                }
            }
            if (sysPermission!=null){
                SysShipTypePermission sysShipTypePermission = new SysShipTypePermission(shipType, sysPermission.getId());
                if(!list.contains(sysShipTypePermission)){
                    list.add(sysShipTypePermission);
                }
            }

        }
        if(permissionParentList.size()>0){
            addParentId(shipType, permissionParentList,list);
        }
        return list;
    }


}
